function F = cmpt_gmm_alt(par_vec,par_set)

%
% Computes moments for the competitive case given parameters
%
%    par_set = [N,nmbr_cmpt,nu_level,a,nu_low,g_rte];
%    par_vec = [b;mc_level; mrkt_size; par_beta1 par_beta2];
%

global flg_otpt par_disct

N = par_set(1);
nmbr_cmpt = par_set(2);
nu_level = par_set(3);
nu_low = par_set(5);

par_beta = [par_vec(3); par_vec(4)];


% all the parameter values must be positive
par_vec = abs(par_vec);
a = par_set(4);
b = par_vec(1) + a;

disp(['Parameter vector is:  ' num2str(par_vec')]);

% defining vector of possible vintage utilities
    g_rte = 1/par_set(6);
    nu_tmp = zeros(nmbr_cmpt+1,1);
    nu_tmp(1) = nu_low;
    nu_tmp(nmbr_cmpt+1) = nu_level;
    for i=nmbr_cmpt:-1:2
        nu_tmp(i) = nu_tmp(i+1)*g_rte;
    end
    nu_vec = nu_tmp;

% Checking to make sure that outside good is least desirable
% If not, then fix
%    t_ptr = nu_tmp>par_set(5);
%    nu_vec = [nu_tmp(1);nu_tmp(t_ptr)];
%    N = length(nu_vec);

% defining vector of marginal costs
    mc_vec = zeros(nmbr_cmpt+1,1);
    mc_vec(1) = 0;
    mc_vec(2:nmbr_cmpt+1) = par_vec(2);

%    disp([nu_vec mc_vec]); pause
    
% Data -- used to compute least distance criterion
% for the price declines, call estimated price
% decline func prc_decline.m
%
% Number of periods (months, half-months, etc)
 data_prd = 4;         
       
% Initializing matrices to record output 
    
%    Entry every period    
%    disp('Entry every period');
    f_ptr = 1:1:nmbr_cmpt+1;

    % setting up the vector of nu/mc
    nu = nu_vec(f_ptr);
    mc = mc_vec(f_ptr);

%    disp([nu mc f_ptr']); pause    

    % Computing equilibrium prices and sales
    prc_rec = nashN(nu,mc,a,b,par_beta);
%    disp('Using NashN_inv'); 
%    prc_rec = nashN_inv(nu,mc,a,b,mrkt_size,par_beta);

    sal_rec = demand(prc_rec,nu,a,b,par_beta);
    
% Computing moments
% step 1 -- pulling out price and sales vectors
% step 2 -- determining appropriate data-prices
% step 3 -- computing least distance criterion
    
    indx = find(sal_rec>0);
    
    prc_tmp = prc_rec(indx);
    sal_tmp = sal_rec(indx);    
 
    % drop outside option and invert sales and price vectors
    m=length(prc_tmp);
    if prc_tmp(1)==0
        prc_vec = flipud(prc_tmp(2:m));
        sal_vec = flipud(sal_tmp(2:m));    
    else
        prc_vec = flipud(prc_tmp);
        sal_vec = flipud(sal_tmp);    
    end        
%    disp('prices and sales'); disp([prc_vec sal_vec]);  pause 

    % PRICES: computing price decline
    % price(1) is highest price
    ll = length(prc_vec);
    p_decline = zeros(ll-1,1);
    for j=2:ll
        p_decline(j-1) = (prc_vec(j)-prc_vec(1))./prc_vec(1);
    end
   
    % SALES
    tmp = sum(sal_vec);
    l_D = length(sal_vec);
    sal_pdf = sal_vec/tmp;
    s_cdf = cumsum(sal_vec);
    sal_cdf = s_cdf/tmp;
%    disp('Sales pdf and cdf'); disp([sal_pdf ssal_cdf]); pause   

    %
    % Moments: matching price declines to data and sal cdf to bounds
    % note: price declines match up with sales from months
    % two and onwards (first month is reference period)
    %
    % Number of periods moment is quadratic
    %

    % bounds on sales cdf           
    salcdf_bnd = [ 0.2163455	0.599715;
                   0.5997151	0.898035;
                   0.898035     1;
                   1            1];

    % determining model - data sales match
    if l_D==data_prd 
        tmp1 = sal_cdf<salcdf_bnd(:,1);
        tmp2 = sal_cdf>salcdf_bnd(:,2);
        salpdf_mtch = sum( tmp1.*(sal_cdf-salcdf_bnd(:,1)).^2 + ...
                           tmp2.*(sal_cdf-salcdf_bnd(:,2)).^2 );        
    elseif l_D>data_prd
        tmp1 = sal_cdf(1:data_prd)>=salcdf_bnd(:,1);
        tmp2 = sal_cdf(1:data_prd)<=salcdf_bnd(:,2);
        salpdf_mtch = sum( tmp1.*(sal_cdf(1:data_prd)-salcdf_bnd(:,1)).^2 + ...
                           tmp2.*(sal_cdf(1:data_prd)-salcdf_bnd(:,2)).^2 );        
    else  
        tmp1 = sal_cdf>=salcdf_bnd(1:l_D,1);
        tmp2 = sal_cdf<=salcdf_bnd(1:l_D,2);
        salpdf_mtch = sum( tmp1.*(sal_cdf-salcdf_bnd(1:l_D,1)).^2 + ...
                           tmp2.*(sal_cdf-salcdf_bnd(1:l_D,2)).^2 );        
    end  
               
    % determining model - data price match           
    sal_pnt = 100*sal_cdf(2:l_D);
    data_decline = price_decline(sal_pnt);
    data_moment1 = data_decline;
    model_moment1 = p_decline;
    % weights for prices are just ones for now
    tmp = ones(length(model_moment1),1);    
    wgt_mat = diag(tmp);
  
    % criterion is price match + sales match + period match
    F = (model_moment1-data_moment1)'*wgt_mat*(model_moment1-data_moment1) + ...
        salpdf_mtch + (l_D-data_prd)^2;
    
    % Display results 
    if(flg_otpt==1)
               
        disp('Price and Sales matrix');
        disp([prc_rec sal_rec]);
        
        disp('Saving price and sales matrices');
%        save prc_sal_resultsaug17 prc_rec sal_rec par_set par_vec
        
        disp(['Criterion is:  ' num2str(F)]);
        disp('Price moments');
        disp([data_decline p_decline]);
        disp('Model sales pdf cdf');
        disp([sal_pdf sal_cdf]);
        disp('Product cycle length moments');
        disp([data_prd l_D]);
        

        l1=length(data_decline); l2=length(p_decline);
        p_grph = zeros(max(l1,l2)+1,2);
        p_grph(1,:) = [100 100];
        for i=1:l1;
            p_grph(i+1,1) = p_grph(1,1)*(1+data_decline(i));
        end
        for i=1:l2;
            p_grph(i+1,2) = p_grph(1,2)*(1+p_decline(i));
        end
        x=1:1:length(p_grph);
        disp('Price:            Data         Model');
        disp([x' p_grph]);
                
        disp('Outside good : Lifecycle of a typical product');
        disp('           Month   Price     Markup   Sales');
        l = length(prc_vec);
        m=(1:1:l)';
        tmp = (prc_vec-mc(2))./mc(2);
        disp([m prc_vec tmp sal_vec]);
 
        tmp = mean(p_decline);
        tmp1 = mean(data_decline);
        disp(['Average price decline (unweighted), data and model:  ' num2str([tmp1 tmp])]);
        
        [l,n] = size(prc_rec);
        vv = (l-2:-1:0)';
        disct_mat = (par_disct.^vv);
        exp_prft = sum( ((prc_rec(2:l,1)-mc_vec(2)).*sal_rec(2:l,1)).^disct_mat );
        disp(['Expected lifetime profits are:'  num2str(exp_prft)]);
        
                
      % Computing income-vintage relationship & welfare
      % Steps: use actual demand to back out marginal guys
      % and then use marginal guys to determine mean income by vintage
      tmp = sal_rec./1.0;
      
      % compute marginal guys for each entry pattern      
      max_prd = length(sal_vec);
      mean_inc = zeros(max_prd,2);

      % entry every period case
      % using beta distribution
      % assumes no sales to outside good
      indx = l-max_prd:1:l;
      mshare = cumsum(tmp(indx,1));
      mrg_con = icdf('beta',mshare,par_beta(1),par_beta(2));
      inc_bnd = mrg_con*(b-a)+a;
      ll =length(inc_bnd);
      % Approximation of mean income
      for i=ll:-1:2
          mean_inc(i+max_prd-ll,1) = (inc_bnd(i)+inc_bnd(i-1))/2;
      end
      % average income
      stp_sze = 0.00001;
      % b/c beta is infty at 0 for par_beta;
      mrg_con(1)=mrg_con(1)+stp_sze;
      for i=2:5
          t1 = mrg_con(i-1):stp_sze:mrg_con(i);
          t2 = pdf('beta',t1,par_beta(1),par_beta(2));
          t3 = t1*t2' / sum(t2);
          mean_inc(i-1,2) = t3*(b-a)+a;
      end      
      disp('Income bounds and mean (roughly) income');
      disp(inc_bnd); disp(mean_inc);
     
      
      
    end
  

 
  
  
  
